# encoding:utf8
''' 2017-7-25 pylint + fixbug '''

import os
import json
import shutil
from pylibs.lpkg import luajit_pack
from pylibs.verpack import chk_resinfo
from pylibs.verpack import VersionSvnDiff

class LuaGame(object):
    ''' Lua游戏使用脚本 '''
    @classmethod
    def lua_lpack(cls, frompath, tarpath, packtype="jitc"):    
        '''
        打包脚本
            @frompath  源脚本目录
            @tarpath   目标目录
            @binpath  脚本文件目录
            @packtype pack类型, "jitc", "luac", "onlyzip"
        '''
        return luajit_pack(frompath, tarpath, packtype)


    @classmethod
    def crt_verinfo_list(cls, respath, tarfile, datatype="json"):
        '''
        生成热更新数据文件
            @respath     源资源路径
            @tarfile     生成的数据文件路径
            @datatype    生成的数据文件类型
        '''
        infolist = chk_resinfo(respath, datatype)
        print (infolist , "--->>>infolist")
        if os.path.isfile(tarfile):
            os.remove(tarfile)
        datafile = file(tarfile, "wb")

        if datatype == "txt":
            for info in infolist:
                exstr = "" if (infolist.index(info) ==
                               len(infolist) - 1) else ","
                datafile.write(info + exstr)
        elif datatype == "json":
            datafile.write(json.dumps(infolist, sort_keys=True, indent=4))
        print ("tarfile-->>", tarfile)
        datafile.close()


    @classmethod
    def svn_difflist(cls, hotpackconf):
        '''生成热更新资源
            @respath 源资源路径
            @snum 开始的版本号
            @enum 结束的版本号
            @verpath 目标资源路径
        '''
        packer = VersionSvnDiff(hotpackconf)
        difflist = packer.chk_dif()
        packer.copy_diffres()

        datapath = os.path.join(hotpackconf["verpath"], hotpackconf["verinfo"])
        datafile = os.path.join(datapath, hotpackconf["verinfo"] + ".data")
        cls.crt_verinfo_list(os.path.join(datapath, "res"), datafile, "json")
        shutil.make_archive(hotpackconf["verinfo"], "zip", datapath)

    @classmethod
    def pack_icon(cls, basetexture, tardir, platform):
        from pylibs.texture import pack_platicon
        pack_platicon(basetexture, tardir, platform)


    @classmethod
    def pack_page(cls, basetexture, tardir, platform):
        from pylibs.texture import pack_platpage
        pack_platpage(basetexture, tardir, platform)


    @classmethod
    def pack_game_pack(cls, basetexture, tartexture, platform):
        from pylibs.texture import pack_platgamepage
        pack_platgamepage(basetexture, tartexture, platform)


if __name__ == "__main__":
    pass
